$(function () {
    var lang = {
        "sProcessing": "处理中...",
        "sLengthMenu": "每页 _MENU_ 项",
        "sZeroRecords": "没有匹配结果",
        "sInfo": "当前显示第 _START_ 至 _END_ 项，共 _TOTAL_ 项。",
        "sInfoEmpty": "当前显示第 0 至 0 项，共 0 项",
        "sInfoFiltered": "(由 _MAX_ 项结果过滤)",
        "sInfoPostFix": "",
        "sSearch": "搜索:",
        "sUrl": "",
        "sEmptyTable": "表中数据为空",
        "sLoadingRecords": "载入中...",
        "sInfoThousands": ",",
        "oPaginate": {
            "sFirst": "首页",
            "sPrevious": "上页",
            "sNext": "下页",
            "sLast": "末页",
            "sJump": "跳转"
        },
        "oAria": {
            "sSortAscending": ": 以升序排列此列",
            "sSortDescending": ": 以降序排列此列"
        }
    };

    var data = [{
        id: 1,
        className: "高等数学",
        teaName: "张三"
    }, {
        id: 2,
        className: "数据结构",
        teaName: "张三"
    }, {
        id: 3,
        className: "Java",
        teaName: "张三"
    }, {
        id: 4,
        className: "C++",
        teaName: "张三"
    }];

    var rowNum = 1;
    $("#main-wrapper").append('<div class="row" id="row' + rowNum + '"></div>')

    $.each(data, function (index, dataItem) {
        // console.log(index)
        // console.log(dataItem)

        if ((index) % 3 === 0) {
            rowNum++;
            $("#main-wrapper").append('<div class="row" id="row' + rowNum + '"></div>')
        }
        $("#row" + rowNum + "").append('<div class="col-md-4">' +
            '<div class="panel panel-white">' +
            '<div class="panel-heading clearfix">' +
            '<h4 class="panel-title"><b>数据结构【张三】</b></h4>' +
            '</div>' +
            '<div class="panel-body">' +
            '查看作业' +
            '</div>' +
            '</div>' +
            '</div>')
        // select_node.append("<option value='" + dataItem.id + "'>" + dataItem.className + "</option>");

    });

    var class_table = $('#class-table');

    class_table.css("border", "none");
    var table = class_table.DataTable({
        language: lang,
        ajax: {
            url: "/student/stushowallclass",
            type: 'POST',
            dataSrc: function (e) {
                console.log(e);
                return e
            }
        },
        bSort: false,
        bLengthChange: false,
        columns: [
            {
                data: "className",
                createdCell: function (td, cellData, rowData, row, col) {
                    $(td).css("background-color", "white");
                    $(td).css("border", "none");
                }
            },
            {data: "classTcName"},
            {data: "id"}],
        aoColumnDefs: [
            {"bVisible": false, "aTargets": [1]},
            {"bVisible": false, "aTargets": [2]}
        ],
        fnCreatedRow: function (nRow, aData, iDataIndex) {
            console.log(aData)
            $('td:eq(0)', nRow).html(
                "<div class='alert alert-info' role='alert'><b>课程：</b>" + aData.className +
                "<button type='button' onclick='applyClass(" + aData.id + "," + iDataIndex +
                ")'  class='btn btn-default btn-sm' style='float: right'>申请加入该课程</button>" +
                "<br><b>老师：</b>" + aData.classTcName +
                "</div>");
        }
    });


})

function applyClass(index, iDataIndex) {
    alert(index)

    $.ajax({
        type: "POST",
        dataType: "json",
        data: {classId: index},
        // contentType: "application/json;charset=UTF-8",
        url: "/student/stuapplyclass",
        success: function (result) {
            console.log(result)
            $.each(result, function (index, dataItem) {
                select_node.append("<option value='" + dataItem.id + "'>" + dataItem.className + "</option>");
            });
            // if (data.flag) {
            //     $("#addModal").modal('hide');
            //
            // } else {
            // $("#name_msg").html(data.msg)
            // }
        }
    });


    // alert(iDataIndex)
    // $('#example2').DataTable().ajax.reload();
}